package tema1;

import java.util.*;

public class Instance {
	private List<AttributeValue> values;
	
	public Instance() {
		values = new ArrayList<AttributeValue>();
	}
	
	public Instance(AttributeValue[] values) {
		this();
		
		if (values == null)
			throw new IllegalArgumentException("Invalid argument");
		
		for (int i = 0; i< values.length; i++)
			this.values.add(values[i]);
	}
	
	public Instance(Instance instance) {
		this();
		
		for (AttributeValue value : instance.getValues()) {
			values.add(value);
		}
	}
	
	public Instance(Instance instance, int exceptAttributeIndex) {
		this();
		
		int i = 0;
		for (AttributeValue value : instance.getValues()) {
			if (i != exceptAttributeIndex)
				values.add(value);
			i++;
		}
	}
	
	public Instance(AttributeSet attributeSet, String[] stringValues) {
		this();
		
		if (attributeSet == null || stringValues == null)
			throw new IllegalArgumentException("Invalid argument");
		
		for (int i = 0; i < attributeSet.size(); i++) {
			if (attributeSet.get(i) instanceof NumericAttribute) {
				values.add(new NumericValue(stringValues[i]));
			} else if (attributeSet.get(i) instanceof SymbolicAttribute) {
				SymbolicAttribute attribute = (SymbolicAttribute) attributeSet.get(i);
				SymbolicValue attributeValue = attribute.addValue(stringValues[i]);
				values.add(attributeValue);
			}
		}
	}
	
	public AttributeValue getValue(int index) {
		if (index < 0 || index > values.size())
			throw new IllegalArgumentException("Invalid index");
		
		return values.get(index);
	}
	
	public List<AttributeValue> getValues() {
		return values;
	}
	
	public void remove(int index) {
		values.remove(index);
	}
	
	public int noAttributes() {
		return values.size();
	}
	
	public String toString() {
		// TODO: use StringBuilder
		String str = "";
		
		for (AttributeValue value: values) {
			str +=  value + " ";
		}
		
		str += "\n";
		
		return str;
	}
}
